package com.rock.upms.svc.common;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import com.rock.common.annotation.ANNBaseService;
import com.rock.common.util.LoadedInfo;

/**
 * 功能： spring容器初始化完成事件 说明：
 * 
 * @author zhousq
 * @version 创建时间：2017-11-01 zhousq2017-11-07：修改onApplicationEvent方法
 */
public class UpmsSvcContextListener implements ApplicationListener<ContextRefreshedEvent> {

	private static Logger _log = LoggerFactory.getLogger(UpmsSvcContextListener.class);

	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		// if(contextRefreshedEvent.getApplicationContext().getDisplayName().equals("Root
		// WebApplicationContext")){
		if (null == contextRefreshedEvent.getApplicationContext().getParent()) {
			_log.debug(">>>>> spring初始化完毕 <<<<<");
			// spring初始化完毕后，通过反射调用所有使用RockBaseDao注解的initMapper方法
			// Map<String, Object> beansWithNotAutowired2ServiceMap =
			// this.applicationContext.getBeansWithAnnotation(com.lvbey.service.annotation.NotAutowired2Service.class);
			ApplicationContext ac = contextRefreshedEvent.getApplicationContext();
			Map<String, Object> baseServices = ac.getBeansWithAnnotation(ANNBaseService.class);
			for (Object service : baseServices.values()) {
				_log.debug(">>>>> {}.initMapper()", service.getClass().getName());
				try {
					Method initMapper = service.getClass().getMethod("initMapper");
					initMapper.invoke(service);
				} catch (Exception e) {
					_log.error("初始化BaseService的initMapper方法异常", e);
					e.printStackTrace();
				}
			}
			// 加载完毕后打印出项目基本信息
			LoadedInfo overInfo = new LoadedInfo();
			overInfo.setWidth(80);
			overInfo.setOutsideLeftMargin(10);
			overInfo.begin();
			overInfo.addRow(null);
			overInfo.addRow("        " + UpmsSvcConfig.getAppName());
			overInfo.addRow(null);
			overInfo.addRow(null);
			overInfo.addRow("version:" + UpmsSvcConfig.getAppVersion());
			overInfo.addRow("updatedate:" + UpmsSvcConfig.getAppUpdateDate());
			overInfo.addRow("developer:" + UpmsSvcConfig.getAppDeveloper());
			overInfo.addRow("database name:" + UpmsSvcConfig.getDatabaseName());
			overInfo.addRow("database type:" + UpmsSvcConfig.getDatabaseType());
			overInfo.addRow("M-jdbc-url:" + UpmsSvcConfig.getMastarJdbcUrl());
			overInfo.addRow("M-username:" + UpmsSvcConfig.getMasterJdbcUsername());
			overInfo.addRow("S-jdbc-url:" + UpmsSvcConfig.getSlaveJdbcUrl());
			overInfo.addRow("S-username:" + UpmsSvcConfig.getSlaveJdbcUsername());
			overInfo.addRow("zookeeper:" + UpmsSvcConfig.getZookeeperIP());
			overInfo.addRow("provider name:" + UpmsSvcConfig.getServiceProviderNname());
			overInfo.addRow("jdk version:" + System.getProperty("java.version"));
			overInfo.addRow(null);
			overInfo.addRow(null);
			Date nowTime = new Date(System.currentTimeMillis());
			SimpleDateFormat formatter = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
			overInfo.addRow("     started time:" + formatter.format(nowTime));
			overInfo.addRow(null);
			overInfo.addRow(null);
			overInfo.end();
		} else {
			_log.debug("===zhousq:ApplicationContextListener======contextRefreshedEvent.getApplicationContext().getParent()=null ");
		}
	}
}
